//#define __DEBUG__
// by Xeno
#define THIS_FILE "fn_onmapsingleclickresp.sqf"
#include "xr_macros.sqf"

private ["_pos", "_ma", "_dist", "_mcol", "_mes", "_nearest", "_distnearest", "_mapos"];
if (!GVARXR(respawn_available)) exitWith {
	GVARXR(has_map_clicked) = nil;
};
if (!GVARXR(followcam)) then {
	__ucdlg1022 ctrlSetText "";
	__ucdlg1022 ctrlSetTextColor [1,0,0,1];
	__ucdlg1022 ctrlCommit 0;
} else {
	__spectdlg1022 ctrlSetText "";
	__spectdlg1022 ctrlSetTextColor [1,0,0,1];
	__spectdlg1022 ctrlCommit 0;
};
_pos = _this;
_pos set [2,0];
_nearest = [];
_distnearest = 1000000000;
{
	_ma = _x select 1;
	_mapos = markerPos _ma;
	_mapos set [2,0];
	_dist = _pos distance _mapos;
	if (_dist < 30 && {_dist < _distnearest}) then {
		_distnearest = _dist;
		_nearest = _x;
	};
} forEach GVARXR(respmarkerar);

__TRACE_2("","_pos","_nearest");

if (count _nearest == 0) exitWith {
	GVARXR(has_map_clicked) = nil;
};

_ma = _nearest select 1;
_mapos = markerPos _ma;
_mapos set [2,0];
_dist = _pos distance _mapos;
__TRACE_2("","_ma","_dist");
_mcol = markerColor _ma;
__TRACE_2("","_dist","_mcol");
if (_dist < 30 && {_mcol == "COLORGREEN"}) exitWith {
	_pos = _mapos;
	__TRACE_1("","_pos");
	call FUNCXR(closeSpectControls);
	GVARXR(has_map_clicked) = nil;
	_pos spawn FUNCXR(MapClickRespawn);
};
_mes = if (_dist < 30) then {_nearest select 3} else {""};
__TRACE_1("","_mes");
if (_mes != "") then {
	if (!GVARXR(followcam)) then {
		__ucdlg1022 ctrlSetText _mes;
		__ucdlg1022 ctrlSetTextColor [1,0,0,1];
		__ucdlg1022 ctrlCommit 0;
	} else {
		__spectdlg1022 ctrlSetText _mes;
		__spectdlg1022 ctrlSetTextColor [1,0,0,1];
		__spectdlg1022 ctrlCommit 0;
	};
};
GVARXR(has_map_clicked) = nil;